package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MakeFriendManager;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.vo.NearUserVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.RecommendUserVo;
import com.itheima.domain.vo.VisitorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MakeFriendController {

    @Autowired
    private MakeFriendManager makeFriendManager;

    /**
     * 查询今日佳人
     */
    @GetMapping("/tanhua/todayBest")
    public RecommendUserVo findRecommendUserByUserId() {
        return makeFriendManager.findRecommendUserByUserId();
    }

    /**
     * 查询推荐好友
     */
    @GetMapping("/tanhua/recommendation")
    public PageBeanVo findRecommendUser( @RequestParam(defaultValue = "1" , name = "page") Integer pageNumber ,
            @RequestParam(defaultValue = "10", name = "pagesize" )Integer pageSize) {
        return makeFriendManager.findRecommendUser(pageNumber, pageSize);
    }

    /**
     * 查询推荐用户详情
     */
    @GetMapping("/tanhua/{id}/personalInfo")
    public RecommendUserVo findPersonalInfo(@PathVariable("id") Long recommendUserId) {
        return makeFriendManager.findPersonalInfo(recommendUserId);
    }

    /**
     * 查询陌生人问题
     */
    @GetMapping("/tanhua/strangerQuestions")
    public String findStrangerQuestion(Long userId) {
        return makeFriendManager.findStrangerQuestion(userId);
    }

    /**
     * 最近访客
     */
    @GetMapping("/movements/visitors")
    public List<VisitorVo> findVisitor(){

        return makeFriendManager.findVisitor();
    }

    /**
     * 上报地理位置
     *
     * @return
     */
    @PostMapping("/baidu/location")
    public Map saveOrUpdateUserLocation(@RequestBody Map<String, String> map) {
        Double latitude = Double.valueOf(map.get("latitude"));
        Double longitude = Double.valueOf(map.get("longitude"));
        String addrStr = map.get("addrStr");
        //System.out.println("上传的位置为：" + map);
        makeFriendManager.saveOrUpdateUserLocation(latitude, longitude, addrStr);
        return null;
    }

    //附近的人
    @GetMapping("/tanhua/search")
    public List<NearUserVo> searchUser(String gender, Long distance) {
        return makeFriendManager.searchUser(gender, distance);

    }

    /**
     * 用户认证
     *  客户端主动访问获得环信的账户
     * @return
     */
    @GetMapping("/huanxin/user")
    public Map<String, String> huanxinUser() {
        Long username = UserHolder.getId();

        Map<String, String> result = new HashMap<>();
        result.put("username", username.toString());
        result.put("password", "123456");
        System.out.println("用户认证：" + result);
        return result;
    }

    /**
     * 打招呼
     */
    @PostMapping("/tanhua/strangerQuestions")
    public Map replyStrangerQuestion(@RequestBody Map<String,String> map) {
        //1.接收参数
        String questionUserId = map.get("userId");
        String reply = map.get("reply");
        //2.调用manager
        makeFriendManager.replyStrangerQuestion(questionUserId,reply);

        return null;
    }

    @PostMapping("/messages/contacts")
    public Map saveContact(@RequestBody Map<String,String> map) {
        //1.接收参数
        String replyUserId = map.get("userId");
        //2.调用manager
        makeFriendManager.saveContact(replyUserId);

        return null;
    }

    /**
     * 查看好友列表
     */
    @GetMapping("/messages/contacts")
    public PageBeanVo findContact( @RequestParam(defaultValue = "1" , name = "page") Integer pageNumber ,
            @RequestParam(defaultValue = "10", name = "pagesize" )Integer pageSize) {

        return makeFriendManager.findContact(pageNumber, pageSize);
    }


    /**
     * 探花查询列表
     */
    @GetMapping("/tanhua/cards")
    public List<RecommendUserVo> findTanhuaCards( @RequestParam(defaultValue = "1" , name = "page") Integer pageNumber ,
            @RequestParam(defaultValue = "10", name = "pagesize" )Integer pageSize) {

        //查询推荐的好友信息
        PageBeanVo pageBeanVo = makeFriendManager.findRecommendUser(1, 20);
        return (List<RecommendUserVo>) pageBeanVo.getItems();
    }







}
